package com.yanqu.road.dao.impl.player;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.player.UserVehicleBone;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UserVehicleBoneDaoImpl extends TempDataObjectDao<UserVehicleBone> {
	public static String getUserVehicleBoneMaxIdSql = "select max(boneId) as maxid from `t_u_vehicle_bone` where `userId`=?";
	public int getUserVehicleBoneMaxId(long userId) {
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(getUserVehicleBoneMaxIdSql);
			int i = 1;
			call.setLong(i++, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				int maxId = rs.getInt("maxid");
				return maxId;
			}
		} catch (Exception e) {
			throw new RuntimeException(e);} finally {
			getDb().closeConnection(call, rs);
		}
		return 0;
	}

	public static String getUserVehicleEquipBoneSql = "select * from `t_u_vehicle_bone` where `userId`=? and isDelete = 0 and vehicleId !=''";
	public Map<Integer, UserVehicleBone> getUserVehicleEquipBone(long userId) {
		Map<Integer, UserVehicleBone> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(getUserVehicleEquipBoneSql);
			int i = 1;
			call.setLong(i++, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserVehicleBone data = parse(rs);
				dataMap.put(data.getBoneId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public static String getUserVehicleBoneSql = "select * from `t_u_vehicle_bone` where `userId`=? and isDelete = 0";
	public Map<Integer, UserVehicleBone> getUserVehicleBone(long userId) {
		Map<Integer, UserVehicleBone> dataMap = new ConcurrentHashMap<>();
		ResultSet rs = null;
		CallableStatement call = null;
		try {
			getDb().setConnName(DbNameStrategy.getUserDb());
			call = getDb().getConnection().prepareCall(getUserVehicleBoneSql);
			int i = 1;
			call.setLong(i++, userId);
			rs = call.executeQuery();
			while (rs.next()) {
				UserVehicleBone data = parse(rs);
				dataMap.put(data.getBoneId(), data);
			}
		} catch (Exception e) {
			throw new RuntimeException(e);} finally {
			getDb().closeConnection(call, rs);
		}
		return dataMap;
	}

	public static String updateUserVehicleBoneSql = "update `t_u_vehicle_bone` set configId=?,skillMap=?,vehicleId=?,equipTime=?,equipPos=?,isDelete=? where userId=? and boneId=?";
	@Override
	protected boolean updateData(UserVehicleBone userVehicleBone) {
		userVehicleBone.setBeginWriteTime();
		getDb().setConnName(DbNameStrategy.getUserDb());
		int i = 1;
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(i++, new DbParameter(Types.INTEGER, userVehicleBone.getConfigId()));
		para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userVehicleBone.getSkillMap())));
		para.put(i++, new DbParameter(Types.VARCHAR, userVehicleBone.getVehicleId()));
		para.put(i++, new DbParameter(Types.BIGINT, userVehicleBone.getEquipTime()));
		para.put(i++, new DbParameter(Types.INTEGER, userVehicleBone.getEquipPos()));
		para.put(i++, new DbParameter(Types.BOOLEAN, userVehicleBone.getIsDelete()));
		para.put(i++, new DbParameter(Types.BIGINT, userVehicleBone.getUserId()));
		para.put(i++, new DbParameter(Types.INTEGER, userVehicleBone.getBoneId()));
		if (getDb().executeNoneQuery(updateUserVehicleBoneSql, para) > 0) {
			return true;
		}
		return false;
	}

	public static String insertUserVehicleBoneSql = "insert into `t_u_vehicle_bone`(userId,boneId,configId,skillMap,vehicleId,equipTime,equipPos,isDelete) values (?,?,?,?,?,?,?,?)";
	@Override
	protected boolean addData(UserVehicleBone userVehicleBone) {
		userVehicleBone.setBeginWriteTime();
		getDb().setConnName(DbNameStrategy.getUserDb());
		int i = 1;
		Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
		para.put(i++, new DbParameter(Types.BIGINT, userVehicleBone.getUserId()));
		para.put(i++, new DbParameter(Types.INTEGER, userVehicleBone.getBoneId()));
		para.put(i++, new DbParameter(Types.INTEGER, userVehicleBone.getConfigId()));
		para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(userVehicleBone.getSkillMap())));
		para.put(i++, new DbParameter(Types.VARCHAR, userVehicleBone.getVehicleId()));
		para.put(i++, new DbParameter(Types.BIGINT, userVehicleBone.getEquipTime()));
		para.put(i++, new DbParameter(Types.INTEGER, userVehicleBone.getEquipPos()));
		para.put(i++, new DbParameter(Types.BOOLEAN, userVehicleBone.getIsDelete()));
		if (getDb().executeNoneQuery(insertUserVehicleBoneSql, para) > 0) {
			return true;
		}
		return false;

	}

	@Override
	protected UserVehicleBone parseData(ResultSet rs) throws SQLException {
		UserVehicleBone userVehicleBone = new UserVehicleBone();
		userVehicleBone.setUserId(rs.getLong("userId"));
		userVehicleBone.setBoneId(rs.getInt("boneId"));
		userVehicleBone.setConfigId(rs.getInt("configId"));
		userVehicleBone.setSkillMap(JSON.parseObject(rs.getString("skillMap"), new TypeReference<java.util.Map<java.lang.Integer, java.lang.Integer>>() {}));
		userVehicleBone.setVehicleId(rs.getString("vehicleId"));
		userVehicleBone.setEquipTime(rs.getLong("equipTime"));
		userVehicleBone.setEquipPos(rs.getInt("equipPos"));
		userVehicleBone.setIsDelete(rs.getBoolean("isDelete"));

		return userVehicleBone;
	}
}
